package com.example.wangshuo.recyclerviewtest_205ws.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.example.wangshuo.recyclerviewtest_205ws.adapter.MyAdapter;
import com.example.wangshuo.recyclerviewtest_205ws.R;
import com.example.wangshuo.recyclerviewtest_205ws.bean.ArticleBean;
import com.example.wangshuo.recyclerviewtest_205ws.iface.ArticleListener;
import com.example.wangshuo.recyclerviewtest_205ws.model.ArticleModel;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;//RecyclerView布局管理
    private MyAdapter myAdapter;//RecyclerView适配器
    private  List<ArticleBean> list=null;//保存数据

    ArticleListener articleListener=new ArticleListener() {
        @Override
        public void onResponse(List<ArticleBean> beanList) {
            list=beanList;//拿到文章数据
            myAdapter.setList(list);
            myAdapter.notifyDataSetChanged();//通知更新
        }

        @Override
        public void onFail(String msg) {

            Toast.makeText(MainActivity.this,"失败："+msg,Toast .LENGTH_SHORT).show();

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initRecyclerView();

        ArticleModel model=new ArticleModel();
        model.getResultList("article",1,"rv1l2tidbomann2qn86nln6ur2",articleListener);
    }
    // 初始化RecyclerView
    private void initRecyclerView() {
        recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
        //创建默认线性布局
        layoutManager=new LinearLayoutManager(this);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //固定每个item高度，提高性能
        recyclerView.setHasFixedSize(true);
        //获取数据
        //list=getData();
        //实例化Adapter
        myAdapter=new MyAdapter(this);
        myAdapter.setList(list);//传进数据
        recyclerView.setAdapter(myAdapter);//绑定

    }
    //获取数据源并返回List
   /* private List<ArticleBean> getData(){
        List<ArticleBean> list=new ArrayList<ArticleBean>();
        ArticleBean articleBean;
        //第一组模拟数据
        articleBean=new ArticleBean();
        articleBean.setId(1);
        articleBean.setName("标题一");
        articleBean.setDescription("描述一");
        articleBean.setUpdate_time("2017-4-13");
        articleBean.setThumb(String.valueOf(R.drawable.f1));
        list.add(articleBean);

        //第二组模拟数据
        articleBean=new ArticleBean();
        articleBean.setId(2);
        articleBean.setName("标题2");
        articleBean.setDescription("描述2");
        articleBean.setUpdate_time("2017-4-14");
        articleBean.setThumb(String.valueOf(R.drawable.f2));
        list.add(articleBean);

        //第三组模拟数据
        articleBean=new ArticleBean();
        articleBean.setId(3);
        articleBean.setName("标题三");
        articleBean.setDescription("描述三");
        articleBean.setUpdate_time("2017-4-15");
        articleBean.setThumb(String.valueOf(R.drawable.f3));
        list.add(articleBean);

        //第四组模拟数据
        articleBean=new ArticleBean();
        articleBean.setId(4);
        articleBean.setName("标题四");
        articleBean.setDescription("描述四");
        articleBean.setUpdate_time("2017-4-16");
        articleBean.setThumb(String.valueOf(R.drawable.f4));
        list.add(articleBean);

        //第五组模拟数据
        articleBean=new ArticleBean();
        articleBean.setId(5);
        articleBean.setName("标题五");
        articleBean.setDescription("描述五");
        articleBean.setUpdate_time("2017-4-17");
        articleBean.setThumb(String.valueOf(R.drawable.f5));
        list.add(articleBean);

        //第六组模拟数据
        articleBean=new ArticleBean();
        articleBean.setId(6);
        articleBean.setName("标题六");
        articleBean.setDescription("描述六");
        articleBean.setUpdate_time("2017-4-18");
        articleBean.setThumb(String.valueOf(R.drawable.f6));
        list.add(articleBean);
        return list;

    }*/
}
